<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\order\Order as OrderModel;
use app\api\model\plus\article\Article;
use app\common\enum\order\OrderPayTypeEnum;
use app\common\model\sms\Sms;
use app\common\model\user\Sms as SmsModel;
use app\api\model\user\UserWeb as UserModel;
use app\api\model\user\RegisterInfo as RegisterInfoModel;
use app\common\model\user\UserInfomation;
use app\JjjController;

/**
 * h5 web用户管理
 */
class Userweb extends JjjController
{

    /**
     * 用户自动登录,默认微信小程序
     */
    public function login()
    {
        $model = new UserModel;
        $user_id = $model->login($this->request->post());
        if($user_id == 0){
            return $this->renderError($model->getError() ?:'登录失败');
        }
        return $this->renderSuccess('',[
            'user_id' => $user_id,
            'token' => $model->getToken()
        ]);
    }

    public function loginByPwd()
    {
        $model = new UserModel;
        $user_id = $model->loginByPwd( $this->request->param() );
        if($user_id == 0){
            return $this->renderError($model->getError() ?:'登录失败');
        }
        $type = $model->field('classify')->find($user_id);
        return $this->renderSuccess('',[
            'user_id' => $user_id,
            'classify'=>$type['classify'],
            'token' => $model->getToken()
        ]);
    }
    /**
     * 注册
     * @return [type] [description]
     */
    public function register()
    {
        $params = $this->request->post();
        $model = new UserModel;
        $user_id = $model->register( $params );
        if($user_id == 0){
            return $this->renderError( $model->getError() ?:'登录失败' );
        }
 
        return $this->renderSuccess('注册成功',[
            'user_id' => $user_id,
            'token' => $model->getToken()
        ]);
    }

    public function registerInfo()
    {
        $params = $this->request->post();
        $model = new RegisterInfoModel;
        $user = $this->getUser();
        $id = $model->add( $user , $params );
        if( $id == 0 ){
            return $this->renderError( $model->getError() ?:'失败' );
        }
        return $this->renderSuccess('成功',[
            'id' => $id,
        ]);
    }

    /**
     * 短信登录
     */
    public function sendCode($mobile,$type)
    {
        //$type = $this->request->param("type");
        $model = new SmsModel();
        if($model->send($mobile, $type)){
            return $this->renderSuccess();
        }
        return $this->renderError($model->getError() ?:'发送失败');
    }

    public function payH5($order_id){
        $user = $this->getUser();
        // 订单详情
        $model = OrderModel::getUserOrderDetail($order_id, $user['user_id']);
        // 构建支付请求
        $payment = OrderModel::onOrderPayment($user, $model, OrderPayTypeEnum::WECHAT, 'payH5');

        $return_Url = urlencode(base_url()."h5/pages/order/myorder/myorder");
        return $this->renderSuccess('',[
            'order' => $model,  // 订单详情
            'mweb_url' => $payment['mweb_url'],
            'return_Url' => $return_Url
        ]);
    }



    public function change_pw()
    {
        $params = $this->request->param();
        $model = new UserModel;
        if($model->change_pwd($params)){
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?:'修改失败');
    }

    //会员认证
    public function user_info()
    {
        $model = new UserInfomation();
        $res = $model->add();
    }



}
